package scau.erps.test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SandPlayFrame {

	private JFrame frame;
	private JSplitPane splitPane;
	private JToolBar toolBar;
	private JPanel timePane, sandPane, decisionPane, sandLeftPane, sandRightPane, copyrightPane,
					comprehensiveCostPane,receivablesPane,cashPane,loanPane,researchPane,marketPane,
					materialOrderPane,materialPane,productPane,orderPane,bigFactoryPane,smFactoryPane,
					beginOfYearPane,middleOfYearPane,endOfYearPane,beginAndMiddlePane,step2_1,step2_2,step2_3;
	private JTextField shortLoan1,shortLoan2,shortLoan3,shortLoan4,longLoan1,longLoan2,longLoan3,longLoan4,payables1,payables2,payables3,payables4,highLoan1,highLoan2,highLoan3,highLoan4,cash,sechdule,team;
	private JButton btn2_1_1,btn2_1_2,btn2_2_1,btn2_3_1;
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SandPlayFrame window = new SandPlayFrame();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public SandPlayFrame() {
		initialize();
	}
    //初始化主面板
	private void initialize() {
		frame = new JFrame("ERP沙盘模拟教学辅助系统");
		frame.setBounds(10, 10, 1024, 768);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(getSplitPane(),BorderLayout.CENTER);		
		frame.getContentPane().add(getToolBar(),BorderLayout.NORTH);
		frame.getContentPane().add(getCopyrightPane(), BorderLayout.SOUTH);
		this.initData();
	}
	//初始化数据。后期从数据库盘面表中中读取数据
	private void initData() {
		shortLoan1.setText("20");
		shortLoan2.setText("20");
		shortLoan3.setText("40");
		shortLoan4.setText("60");
		payables1.setText("5");
		payables2.setText("0");
		payables3.setText("6");
		payables4.setText("0");	
		sechdule.setText("Y2Q3");
		team.setText("2");
	}
	//分隔面板
	private JSplitPane getSplitPane(){
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,getSandPane(),getDecisionPane());
		splitPane.setDividerLocation(600);		
		return splitPane;		
	}
	//按钮栏
	private JToolBar getToolBar(){
		toolBar = new JToolBar();
		JButton btnzz = new JButton("申请注资");
		JButton btngld = new JButton("申请高利贷");
		JButton btnpc = new JButton("申请破产");
		JButton btncb = new JButton("财务报表");
		JButton btnpb = new JButton("盘面报表");
		JButton btnph = new JButton("业绩排行");
		toolBar.add(btnzz);
		toolBar.add(btngld);
		toolBar.add(btnpc);
		toolBar.addSeparator(); 
		toolBar.add(btncb);
		toolBar.add(btnpb);
		toolBar.add(btnph);
		return toolBar;
		
	}
	//状态栏
	private JPanel getCopyrightPane() {
		copyrightPane = new JPanel();
		JLabel label = new JLabel("版权所有：华南农业大学信息学院 ");
		copyrightPane.add(label);
		return copyrightPane;
	}
	//沙盘盘面
	private JPanel getSandPane(){
		sandPane = new JPanel();
		sandPane.setLayout(new GridLayout(1,2,10,5));
		sandPane.add(getSandLeftPane());
		sandPane.add(getSandRightPane());
		return sandPane;
	}
	//沙盘左盘面
	private JPanel getSandLeftPane() {
		sandLeftPane = new JPanel();
		sandLeftPane.setLayout(new GridLayout(6,1));
		sandLeftPane.add(getComprehensiveCostPane());
		sandLeftPane.add(getReceivablesPane());
		sandLeftPane.add(getCashPane());
		sandLeftPane.add(getLoanPane());
		sandLeftPane.add(getResearchPane());
		sandLeftPane.add(getMarketPane());
		return sandLeftPane;
	}
	//沙盘右盘面
	private JPanel getSandRightPane() {
		sandRightPane = new JPanel();
		sandRightPane.setLayout(new GridLayout(6,1));		
		sandRightPane.add(getMaterialOrderPane());
		sandRightPane.add(getMaterialPane());
		sandRightPane.add(getBigFactoryPane());
		sandRightPane.add(getSmFactoryPane());
		sandRightPane.add(getProductPane());
		sandRightPane.add(getOrderPane());
		return sandRightPane;
	}
	//综合费用
	private JPanel getComprehensiveCostPane(){
		comprehensiveCostPane = new JPanel();
		comprehensiveCostPane.setBorder(BorderFactory.createTitledBorder("综合费用"));
		return comprehensiveCostPane;
	}
	//原材料订单
	private JPanel getMaterialOrderPane(){
		materialOrderPane = new JPanel();
		materialOrderPane.setBorder(BorderFactory.createTitledBorder("原材料订单"));
		return materialOrderPane;
	}
	//应收
	private JPanel getReceivablesPane() {
		receivablesPane = new JPanel();
		receivablesPane.setBorder(BorderFactory.createTitledBorder("应收款"));
		return receivablesPane;
	}
	//原材料
	private JPanel getMaterialPane() {
		materialPane = new JPanel();
		materialPane.setBorder(BorderFactory.createTitledBorder("原材料"));
		return materialPane;
	}
	//现金
	private JPanel getCashPane() {
		cashPane = new JPanel();
		cashPane.setBorder(BorderFactory.createTitledBorder("现金"));
		cash = new JTextField(5);
		cash.setText("30");
		cash.setEditable(false);
		cashPane.add(cash);		
		return cashPane;
	}
	//成品库
	private JPanel getProductPane() {
		productPane = new JPanel();
		productPane.setBorder(BorderFactory.createTitledBorder("成品库"));
		return productPane;
	}
	//贷款
	private JPanel getLoanPane() {
		loanPane = new JPanel();
		loanPane.setBorder(BorderFactory.createTitledBorder("贷款"));
		loanPane.setLayout(new GridLayout(5,4,40,2));
		JLabel shortLoanLabel= new JLabel("短贷");
		shortLoan1 = new JTextField(2);
		shortLoan2 = new JTextField(2);
		shortLoan3 = new JTextField(2);
		shortLoan4 = new JTextField(2);
		UIUtil.setNoEditing(shortLoan1, shortLoan2, shortLoan3,shortLoan4);	
		JLabel longLoanLabel= new JLabel("长贷");
		longLoan1 = new JTextField(3);
		longLoan2 = new JTextField(3);
		longLoan3 = new JTextField(3);
		longLoan4 = new JTextField(3);
		JLabel highLoanLabel= new JLabel("高利贷");
		highLoan1 = new JTextField(2);
		highLoan2 = new JTextField(2);
		highLoan3 = new JTextField(2);
		highLoan4 = new JTextField(2);
		JLabel payablesLabel= new JLabel("应付款");
		payables1 = new JTextField(2);
		payables2 = new JTextField(2);
		payables3 = new JTextField(2);
		payables4 = new JTextField(2);
		UIUtil.setNoEditing(payables1, payables2, payables3,payables4);	
		loanPane.add(shortLoanLabel);
		loanPane.add(longLoanLabel);
		loanPane.add(highLoanLabel);
		loanPane.add(payablesLabel);
		loanPane.add(shortLoan1);
		loanPane.add(longLoan1);
		loanPane.add(highLoan1);
		loanPane.add(payables1);
		loanPane.add(shortLoan2);
		loanPane.add(longLoan2);
		loanPane.add(highLoan2);
		loanPane.add(payables2);
		loanPane.add(shortLoan3);
		loanPane.add(longLoan3);
		loanPane.add(highLoan3);
		loanPane.add(payables3);
		loanPane.add(shortLoan4);	
		loanPane.add(longLoan4);			
		loanPane.add(highLoan4);
		loanPane.add(payables4);	
		return loanPane;
	}
	//订单
	private JPanel getOrderPane() {
		orderPane = new JPanel();
		orderPane.setBorder(BorderFactory.createTitledBorder("订单"));
		return orderPane;
	}
	//产品研发
	private JPanel getResearchPane(){
		researchPane = new JPanel();
		researchPane.setBorder(BorderFactory.createTitledBorder("产品研发"));
		return researchPane;		
	}
	//大厂房
	private JPanel getBigFactoryPane(){
		bigFactoryPane = new JPanel();
		bigFactoryPane.setBorder(BorderFactory.createTitledBorder("大厂房"));
		return bigFactoryPane;		
	}
	//ISO认证/市场开拓
	private JPanel getMarketPane(){
		marketPane = new JPanel();
		marketPane.setBorder(BorderFactory.createTitledBorder("ISO认证/市场开拓"));
		return marketPane;		
	}
	//中小厂房
	private JPanel getSmFactoryPane(){
		smFactoryPane = new JPanel();
		smFactoryPane.setBorder(BorderFactory.createTitledBorder("中/小厂房"));
		return smFactoryPane;		
	}
	
	//决策面板
	private JPanel getDecisionPane(){
		decisionPane = new JPanel();
		//decisionPane.setLayout(new GridLayout(4,1));
		decisionPane.setLayout(new BorderLayout());
		decisionPane.add(getTimePane(),BorderLayout.NORTH);
		decisionPane.add(getBeginAndMiddlePane(),BorderLayout.CENTER);
		decisionPane.add(getEndOfYearPane(),BorderLayout.SOUTH);
		return decisionPane;
	}
	
	//进程
	private JPanel getTimePane(){
		timePane = new JPanel();
		timePane.setBorder(BorderFactory.createTitledBorder("进程"));
		JLabel label1 = new JLabel("当前季度: ");
		timePane.add(label1);
		sechdule = new JTextField(4);
		timePane.add(sechdule);
		JLabel label2 = new JLabel("小组号: ");
		timePane.add(label2);
		team = new JTextField(3);
		timePane.add(team);
		return timePane;	
		
	}
	//布局中间容器（年初/年中）
	private JPanel getBeginAndMiddlePane(){
		beginAndMiddlePane = new JPanel();
		beginAndMiddlePane.setLayout(new BorderLayout());
		beginAndMiddlePane.add(getBeginOfYearPane(),BorderLayout.NORTH);
		beginAndMiddlePane.add(getMiddleOfYearPane(),BorderLayout.CENTER);
		return beginAndMiddlePane;
		
	}
	//年初
	private JPanel getBeginOfYearPane(){
		beginOfYearPane = new JPanel();
		beginOfYearPane.setBorder(BorderFactory.createTitledBorder("年初"));
		beginOfYearPane.setPreferredSize(new Dimension(400, 100));
		return beginOfYearPane;
	}
	//年中
	private JPanel getMiddleOfYearPane(){
		middleOfYearPane = new JPanel();
		middleOfYearPane.setBorder(BorderFactory.createTitledBorder("年中"));
		middleOfYearPane.setLayout(new GridLayout(11,1));
		middleOfYearPane.add(getStep2_1());
		middleOfYearPane.add(getStep2_2());
		middleOfYearPane.add(getStep2_3());
		return middleOfYearPane;
	}

	//年末
	private JPanel getEndOfYearPane(){
		endOfYearPane = new JPanel();
		endOfYearPane.setBorder(BorderFactory.createTitledBorder("年末"));
		endOfYearPane.setPreferredSize(new Dimension(400, 150));
		return endOfYearPane;
	}

	//贷款按钮
	private JPanel getStep2_1(){
		step2_1 = new JPanel();
		step2_1.setLayout(new FlowLayout(FlowLayout.LEFT));
		btn2_1_1 = new JButton("更新短期贷款");
		btn2_1_2 = new JButton("新增短期贷款");
		btn2_1_2.setEnabled(false);
		btn2_1_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int temp1 = Integer.parseInt(shortLoan1.getText());
				int temp2 = (int) (temp1/20);
				int temp3 = Integer.parseInt(cash.getText());				
				if (temp1>0) {
					if (temp3<(temp1+temp2)) {
						JOptionPane.showMessageDialog(null, "你的现金不足还款付息，请先融资", "错误",JOptionPane.ERROR_MESSAGE);						
					} else{
						String info = "还贷款"+temp1+"百万,"+"支付利息"+temp2+"百万.";
						JOptionPane.showMessageDialog(null, info, "提醒",JOptionPane.INFORMATION_MESSAGE);	
						cash.setText(Integer.toString(temp3-temp1-temp2));
						UIUtil.update(shortLoan1, shortLoan2, shortLoan3, shortLoan4);		
						changeButtonState(btn2_1_1, btn2_1_2);
					}									
				} else{
					 UIUtil.update(shortLoan1, shortLoan2, shortLoan3, shortLoan4);	
					 changeButtonState(btn2_1_1, btn2_1_2);
				}
			}
		});
		btn2_1_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Object[] obj2 ={ "20", "40", "60"};  
				String s = (String) JOptionPane.showInputDialog(null,"请选择贷款额度:\n", "申请短贷", JOptionPane.PLAIN_MESSAGE, null, obj2, "20");
				if(s==null){
					changeButtonState(btn2_1_2, btn2_2_1);
				} else {
					String info = "申请贷款"+s+"百万";
					JOptionPane.showMessageDialog(null, info, "提醒",JOptionPane.INFORMATION_MESSAGE);	
					int temp1 = Integer.parseInt(cash.getText());
					int temp2 = Integer.parseInt(s);
					cash.setText(Integer.toString(temp1+temp2));
					shortLoan4.setText(s);
					changeButtonState(btn2_1_2, btn2_2_1);
					//此处可以进行数据库操作，往短贷表添加一条记录。
				}
			}
		});	
		step2_1.add(new JLabel("1"));
		step2_1.add(btn2_1_1);
		step2_1.add(btn2_1_2);
		return step2_1;
	}
	//更新应付款按钮
	private JPanel getStep2_2(){
		step2_2 = new JPanel();
		step2_2.setLayout(new FlowLayout(FlowLayout.LEFT));
		btn2_2_1 = new JButton("更新应付款");
		btn2_2_1.setEnabled(false);
		btn2_2_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int temp1 = Integer.parseInt(payables1.getText());
				int temp2 = Integer.parseInt(cash.getText());				
				if (temp1>0) {
					if (temp2<temp1) {
						JOptionPane.showMessageDialog(null, "你的现金不足支付应付款，请先融资", "错误",JOptionPane.ERROR_MESSAGE);						
					} else{
						String info = "归还应付款"+temp1+"百万";
						JOptionPane.showMessageDialog(null, info, "提醒",JOptionPane.INFORMATION_MESSAGE);
						cash.setText(Integer.toString(temp2-temp1));
						UIUtil.update(payables1, payables2, payables3, payables4);
						changeButtonState(btn2_2_1, btn2_3_1);
					}									
				} else{
					UIUtil.update(payables1, payables2, payables3, payables4);
					 changeButtonState(btn2_2_1, btn2_3_1);
				}
			}
		});
		step2_2.add(new JLabel("2"));
		step2_2.add(btn2_2_1);
		return step2_2;
	}
	//原材料订单按钮
	private JPanel getStep2_3(){
		step2_3 = new JPanel();
		step2_3.setLayout(new FlowLayout(FlowLayout.LEFT));
		btn2_3_1 = new JButton("更新原材料订单");
		btn2_3_1.setEnabled(false);
		btn2_3_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
			}
		});	
		step2_3.add(new JLabel("3"));
		step2_3.add(btn2_3_1);
		return step2_3;
	}

	//下一步时更改按钮状态
	private void changeButtonState(JButton btn1, JButton btn2){
		btn1.setEnabled(false);
		btn2.setEnabled(true);
	}
}
